/** @format */

// Author 区区电脑
// Email 1573580882@qq.com
// Date 2022-08-03
// Copyright © 2022 gys. All rights reserved.
import NProgress from "nprogress";
import LocalDB from "@/utils/LocalDB";
import router from "@/router/index";

const workbenches = {
    componentName: "workbenches",
    id: "workbenches",
    query: {
        page: "/workbenches",
        titleName: "工作台"
    },
    sourceRoute: {
        fullPath: "/?page=/workbenches&titleName=工作台",
        query: {
            page: "/workbenches",
            titleName: "工作台"
        },
    }
}
const user = {
    state: {
        // 已经注册过的组件
        registerComponent: {workbenches: "workbenches"},
        // 已经打开的页面
        openedPages: [workbenches],
        // 当前页面组件名
        activePageComponentName: "",
        // 刷新页面
        isReloading: false,
    },
    mutations: {
        registerComponent(state, value) {
            state.registerComponent[value] = value;
        },
        openedPages(state, value) {
            const index = state.openedPages.findIndex(item => item.componentName == value.componentName);
            if (index > -1) {
                state.openedPages[index] = value;
            } else {
                state.openedPages.push(value)
            }

        },
        activePageComponentName(state, value) {
            state.activePageComponentName = value;
        },
        isReloading(state) {
            NProgress.start();
            state.isReloading = true;
            setTimeout(() => {
                state.isReloading = false;
                NProgress.done();
            }, 100);
        },
        closePage(state, value) {
            const pages = state.openedPages;
            const index = pages.findIndex(item => item.componentName === value);
            // 关闭当前，如果关闭的是当前选中的页面则 跳转到 下一个或上一个，
            if (state.activePageComponentName === value) {
                const next = pages[index + 1] || pages[index - 1] || workbenches;
                state.activePageComponentName = next.componentName;
                router.push(next.sourceRoute.fullPath)
            }
            state.openedPages.splice(index, 1);
        },
        closeLeft(state, value) {
            // 一定是以当前选中作为锚点
            const pages = state.openedPages;
            const index = pages.findIndex(item => item.componentName === state.activePageComponentName);
            state.openedPages.splice(1, index - 1); // 从1 开始是为了保留最开始的 工作台，从 -1 结束是为了保留当前
        },
        closeRight(state, value) {
            // 一定是以当前选中作为锚点
            const pages = state.openedPages;
            const index = pages.findIndex(item => item.componentName === state.activePageComponentName);
            state.openedPages.splice(index + 1,); // 从 index + 1 开始是为了保留当前
        },
        closeAll(state, value) {
            // 直接设置工作台为当前，然后删除所有其他项
            router.push(workbenches.sourceRoute.fullPath);
            state.openedPages.splice(1);
        }
    },
    actions: {
        registerComponent({commit}, value) {
            commit("registerComponent", value)
        },
        openedPages({commit}, value) {
            commit("openedPages", value);
        },
        activePageComponentName({commit}, value) {
            commit("activePageComponentName", value);
        },
        isReloading({commit}, value) {
            commit("isReloading", value);
        },
        closePage({commit}, value) {
            commit("closePage", value);
        },
        closeLeft({commit}, value) {
            commit("closeLeft", value);
        },
        closeRight({commit}, value) {
            commit("closeRight", value);
        },
        closeOther({commit}, value) {
            console.log("关闭其他: ", value)
            // 一定是以当前选中作为锚点
            // 先关闭右侧，也就是数组后面的
            commit("closeRight", value);
            // 再关闭左侧，也就是数组前面的(不包含当前 和 工作台)
            commit("closeLeft", value);
        },
        closeAll({commit}, value) {
            commit("closeAll", value);
        },
    },
};

export default user;
